/******************************************************************************
秋夜静，独坐对残灯。
啼笑非非谁识我，坐行梦梦尽缘君，何所慰消沉。
风卷雨，雨复卷侬心。
心似欲随风雨去，茫茫大海任浮沉。
无爱亦无恨。
******************************************************************************/
#include "Effects/kbRGB_Int/kbRGB_XY2Angle.h"
#include <stdio.h>
#include <math.h>

static const uint8_t XY2Angle[KBRGB_ANGLE_SIZE*2][KBRGB_ANGLE_SIZE*2] = {
{0x00, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3},
{0x00, 0x59, 0x7E, 0x8F, 0x97, 0x9D, 0xA1, 0xA3, 0xA5, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAB, 0xAC, 0xAC, 0xAD, 0xAD, 0xAD, 0xAE, 0xAE, 0xAE, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB0, 0xB1, 0xB1, 0xB1},
{0x00, 0x35, 0x59, 0x70, 0x7E, 0x88, 0x8F, 0x94, 0x97, 0x9A, 0x9D, 0x9F, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA7, 0xA8, 0xA9, 0xA9, 0xAA, 0xAA, 0xAA, 0xAB, 0xAB, 0xAB, 0xAC, 0xAC, 0xAC, 0xAC, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAD, 0xAE, 0xAE, 0xAE},
{0x00, 0x24, 0x43, 0x59, 0x6A, 0x76, 0x7E, 0x85, 0x8A, 0x8F, 0x92, 0x95, 0x97, 0x9A, 0x9B, 0x9D, 0x9E, 0x9F, 0xA1, 0xA2, 0xA2, 0xA3, 0xA4, 0xA5, 0xA5, 0xA6, 0xA6, 0xA7, 0xA7, 0xA8, 0xA8, 0xA8, 0xA9, 0xA9, 0xA9, 0xAA, 0xAA, 0xAA, 0xAA, 0xAB, 0xAB, 0xAB},
{0x00, 0x1C, 0x35, 0x49, 0x59, 0x66, 0x70, 0x78, 0x7E, 0x84, 0x88, 0x8C, 0x8F, 0x91, 0x94, 0x96, 0x97, 0x99, 0x9A, 0x9C, 0x9D, 0x9E, 0x9F, 0xA0, 0xA1, 0xA1, 0xA2, 0xA3, 0xA3, 0xA4, 0xA4, 0xA5, 0xA5, 0xA6, 0xA6, 0xA6, 0xA7, 0xA7, 0xA7, 0xA8, 0xA8, 0xA8},
{0x00, 0x16, 0x2B, 0x3D, 0x4D, 0x59, 0x64, 0x6C, 0x73, 0x79, 0x7E, 0x83, 0x86, 0x89, 0x8C, 0x8F, 0x91, 0x93, 0x94, 0x96, 0x97, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 0x9F, 0xA0, 0xA1, 0xA1, 0xA2, 0xA2, 0xA3, 0xA3, 0xA4, 0xA4, 0xA5, 0xA5, 0xA5, 0xA6},
{0x00, 0x12, 0x24, 0x35, 0x43, 0x4F, 0x59, 0x62, 0x6A, 0x70, 0x76, 0x7A, 0x7E, 0x82, 0x85, 0x88, 0x8A, 0x8D, 0x8F, 0x90, 0x92, 0x94, 0x95, 0x96, 0x97, 0x99, 0x9A, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9E, 0x9F, 0x9F, 0xA0, 0xA1, 0xA1, 0xA2, 0xA2, 0xA2, 0xA3},
{0x00, 0x10, 0x1F, 0x2E, 0x3B, 0x47, 0x51, 0x59, 0x61, 0x68, 0x6E, 0x73, 0x77, 0x7B, 0x7E, 0x81, 0x84, 0x87, 0x89, 0x8B, 0x8D, 0x8F, 0x90, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9C, 0x9D, 0x9D, 0x9E, 0x9F, 0x9F, 0xA0, 0xA0},
{0x00, 0x0E, 0x1C, 0x29, 0x35, 0x40, 0x49, 0x52, 0x59, 0x60, 0x66, 0x6B, 0x70, 0x74, 0x78, 0x7B, 0x7E, 0x81, 0x84, 0x86, 0x88, 0x8A, 0x8C, 0x8D, 0x8F, 0x90, 0x91, 0x92, 0x94, 0x95, 0x96, 0x97, 0x97, 0x98, 0x99, 0x9A, 0x9A, 0x9B, 0x9C, 0x9C, 0x9D, 0x9D},
{0x00, 0x0C, 0x19, 0x24, 0x2F, 0x3A, 0x43, 0x4B, 0x53, 0x59, 0x60, 0x65, 0x6A, 0x6E, 0x72, 0x76, 0x79, 0x7C, 0x7E, 0x81, 0x83, 0x85, 0x87, 0x89, 0x8A, 0x8C, 0x8D, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x97, 0x98, 0x99, 0x9A, 0x9A, 0x9B},
{0x00, 0x0B, 0x16, 0x21, 0x2B, 0x35, 0x3D, 0x45, 0x4D, 0x53, 0x59, 0x5F, 0x64, 0x68, 0x6C, 0x70, 0x73, 0x77, 0x79, 0x7C, 0x7E, 0x81, 0x83, 0x85, 0x86, 0x88, 0x89, 0x8B, 0x8C, 0x8D, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x94, 0x95, 0x96, 0x97, 0x97, 0x98},
{0x00, 0x0A, 0x14, 0x1E, 0x27, 0x30, 0x39, 0x40, 0x48, 0x4E, 0x54, 0x59, 0x5E, 0x63, 0x67, 0x6B, 0x6E, 0x72, 0x75, 0x77, 0x7A, 0x7C, 0x7E, 0x80, 0x82, 0x84, 0x86, 0x87, 0x89, 0x8A, 0x8B, 0x8C, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x92, 0x93, 0x94, 0x95, 0x95},
{0x00, 0x09, 0x12, 0x1C, 0x24, 0x2D, 0x35, 0x3C, 0x43, 0x49, 0x4F, 0x55, 0x59, 0x5E, 0x62, 0x66, 0x6A, 0x6D, 0x70, 0x73, 0x76, 0x78, 0x7A, 0x7C, 0x7E, 0x80, 0x82, 0x84, 0x85, 0x87, 0x88, 0x89, 0x8A, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x90, 0x91, 0x92, 0x93},
{0x00, 0x08, 0x11, 0x19, 0x22, 0x2A, 0x31, 0x38, 0x3F, 0x45, 0x4B, 0x50, 0x55, 0x59, 0x5E, 0x62, 0x65, 0x69, 0x6C, 0x6F, 0x71, 0x74, 0x76, 0x79, 0x7B, 0x7D, 0x7E, 0x80, 0x82, 0x83, 0x85, 0x86, 0x87, 0x88, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x8F, 0x90},
{0x00, 0x08, 0x10, 0x18, 0x1F, 0x27, 0x2E, 0x35, 0x3B, 0x41, 0x47, 0x4C, 0x51, 0x55, 0x59, 0x5D, 0x61, 0x65, 0x68, 0x6B, 0x6E, 0x70, 0x73, 0x75, 0x77, 0x79, 0x7B, 0x7D, 0x7E, 0x80, 0x81, 0x83, 0x84, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E},
{0x00, 0x07, 0x0F, 0x16, 0x1D, 0x24, 0x2B, 0x32, 0x38, 0x3D, 0x43, 0x48, 0x4D, 0x51, 0x56, 0x59, 0x5D, 0x61, 0x64, 0x67, 0x6A, 0x6C, 0x6F, 0x71, 0x73, 0x76, 0x78, 0x79, 0x7B, 0x7D, 0x7E, 0x80, 0x81, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B},
{0x00, 0x07, 0x0E, 0x15, 0x1C, 0x22, 0x29, 0x2F, 0x35, 0x3A, 0x40, 0x45, 0x49, 0x4E, 0x52, 0x56, 0x59, 0x5D, 0x60, 0x63, 0x66, 0x69, 0x6B, 0x6E, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7A, 0x7B, 0x7D, 0x7E, 0x80, 0x81, 0x82, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89},
{0x00, 0x06, 0x0D, 0x14, 0x1A, 0x20, 0x26, 0x2C, 0x32, 0x37, 0x3C, 0x41, 0x46, 0x4A, 0x4E, 0x52, 0x56, 0x59, 0x5D, 0x60, 0x63, 0x66, 0x68, 0x6B, 0x6D, 0x6F, 0x71, 0x73, 0x75, 0x77, 0x78, 0x7A, 0x7C, 0x7D, 0x7E, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86},
{0x00, 0x06, 0x0C, 0x12, 0x19, 0x1F, 0x24, 0x2A, 0x2F, 0x35, 0x3A, 0x3E, 0x43, 0x47, 0x4B, 0x4F, 0x53, 0x56, 0x59, 0x5D, 0x60, 0x62, 0x65, 0x67, 0x6A, 0x6C, 0x6E, 0x70, 0x72, 0x74, 0x76, 0x77, 0x79, 0x7A, 0x7C, 0x7D, 0x7E, 0x80, 0x81, 0x82, 0x83, 0x84},
{0x00, 0x06, 0x0C, 0x11, 0x17, 0x1D, 0x23, 0x28, 0x2D, 0x32, 0x37, 0x3C, 0x40, 0x44, 0x48, 0x4C, 0x50, 0x53, 0x56, 0x59, 0x5C, 0x5F, 0x62, 0x64, 0x67, 0x69, 0x6B, 0x6D, 0x6F, 0x71, 0x73, 0x74, 0x76, 0x78, 0x79, 0x7B, 0x7C, 0x7D, 0x7E, 0x80, 0x81, 0x82},
{0x00, 0x05, 0x0B, 0x11, 0x16, 0x1C, 0x21, 0x26, 0x2B, 0x30, 0x35, 0x39, 0x3D, 0x42, 0x45, 0x49, 0x4D, 0x50, 0x53, 0x57, 0x59, 0x5C, 0x5F, 0x61, 0x64, 0x66, 0x68, 0x6A, 0x6C, 0x6E, 0x70, 0x72, 0x73, 0x75, 0x77, 0x78, 0x79, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F},
{0x00, 0x05, 0x0A, 0x10, 0x15, 0x1A, 0x1F, 0x24, 0x29, 0x2E, 0x32, 0x37, 0x3B, 0x3F, 0x43, 0x47, 0x4A, 0x4D, 0x51, 0x54, 0x57, 0x59, 0x5C, 0x5F, 0x61, 0x63, 0x66, 0x68, 0x6A, 0x6C, 0x6E, 0x6F, 0x71, 0x73, 0x74, 0x76, 0x77, 0x78, 0x7A, 0x7B, 0x7C, 0x7D},
{0x00, 0x05, 0x0A, 0x0F, 0x14, 0x19, 0x1E, 0x23, 0x27, 0x2C, 0x30, 0x35, 0x39, 0x3D, 0x40, 0x44, 0x48, 0x4B, 0x4E, 0x51, 0x54, 0x57, 0x59, 0x5C, 0x5E, 0x61, 0x63, 0x65, 0x67, 0x69, 0x6B, 0x6D, 0x6E, 0x70, 0x72, 0x73, 0x75, 0x76, 0x77, 0x79, 0x7A, 0x7B},
{0x00, 0x04, 0x09, 0x0E, 0x13, 0x18, 0x1D, 0x21, 0x26, 0x2A, 0x2E, 0x33, 0x37, 0x3A, 0x3E, 0x42, 0x45, 0x48, 0x4C, 0x4F, 0x52, 0x54, 0x57, 0x59, 0x5C, 0x5E, 0x61, 0x63, 0x65, 0x67, 0x69, 0x6A, 0x6C, 0x6E, 0x6F, 0x71, 0x72, 0x74, 0x75, 0x76, 0x78, 0x79},
{0x00, 0x04, 0x09, 0x0E, 0x12, 0x17, 0x1C, 0x20, 0x24, 0x29, 0x2D, 0x31, 0x35, 0x38, 0x3C, 0x40, 0x43, 0x46, 0x49, 0x4C, 0x4F, 0x52, 0x55, 0x57, 0x59, 0x5C, 0x5E, 0x60, 0x62, 0x64, 0x66, 0x68, 0x6A, 0x6B, 0x6D, 0x6F, 0x70, 0x72, 0x73, 0x74, 0x76, 0x77},
{0x00, 0x04, 0x09, 0x0D, 0x12, 0x16, 0x1A, 0x1F, 0x23, 0x27, 0x2B, 0x2F, 0x33, 0x36, 0x3A, 0x3D, 0x41, 0x44, 0x47, 0x4A, 0x4D, 0x50, 0x52, 0x55, 0x57, 0x59, 0x5C, 0x5E, 0x60, 0x62, 0x64, 0x66, 0x68, 0x69, 0x6B, 0x6C, 0x6E, 0x6F, 0x71, 0x72, 0x73, 0x75},
{0x00, 0x04, 0x08, 0x0D, 0x11, 0x15, 0x19, 0x1E, 0x22, 0x26, 0x2A, 0x2D, 0x31, 0x35, 0x38, 0x3B, 0x3F, 0x42, 0x45, 0x48, 0x4B, 0x4D, 0x50, 0x52, 0x55, 0x57, 0x59, 0x5C, 0x5E, 0x60, 0x62, 0x64, 0x65, 0x67, 0x69, 0x6A, 0x6C, 0x6D, 0x6F, 0x70, 0x71, 0x73},
{0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x19, 0x1D, 0x21, 0x24, 0x28, 0x2C, 0x2F, 0x33, 0x36, 0x3A, 0x3D, 0x40, 0x43, 0x46, 0x49, 0x4B, 0x4E, 0x50, 0x53, 0x55, 0x57, 0x59, 0x5C, 0x5E, 0x60, 0x61, 0x63, 0x65, 0x67, 0x68, 0x6A, 0x6B, 0x6D, 0x6E, 0x6F, 0x71},
{0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C, 0x1F, 0x23, 0x27, 0x2A, 0x2E, 0x31, 0x35, 0x38, 0x3B, 0x3E, 0x41, 0x44, 0x47, 0x49, 0x4C, 0x4E, 0x51, 0x53, 0x55, 0x57, 0x59, 0x5C, 0x5D, 0x5F, 0x61, 0x63, 0x65, 0x66, 0x68, 0x69, 0x6B, 0x6C, 0x6E, 0x6F},
{0x00, 0x03, 0x07, 0x0B, 0x0F, 0x13, 0x17, 0x1B, 0x1E, 0x22, 0x26, 0x29, 0x2C, 0x30, 0x33, 0x36, 0x39, 0x3C, 0x3F, 0x42, 0x45, 0x47, 0x4A, 0x4C, 0x4F, 0x51, 0x53, 0x55, 0x57, 0x59, 0x5B, 0x5D, 0x5F, 0x61, 0x63, 0x64, 0x66, 0x67, 0x69, 0x6A, 0x6C, 0x6D},
{0x00, 0x03, 0x07, 0x0B, 0x0F, 0x12, 0x16, 0x1A, 0x1D, 0x21, 0x24, 0x28, 0x2B, 0x2E, 0x32, 0x35, 0x38, 0x3B, 0x3D, 0x40, 0x43, 0x45, 0x48, 0x4A, 0x4D, 0x4F, 0x51, 0x53, 0x56, 0x58, 0x59, 0x5B, 0x5D, 0x5F, 0x61, 0x62, 0x64, 0x65, 0x67, 0x68, 0x6A, 0x6B},
{0x00, 0x03, 0x07, 0x0B, 0x0E, 0x12, 0x15, 0x19, 0x1C, 0x20, 0x23, 0x27, 0x2A, 0x2D, 0x30, 0x33, 0x36, 0x39, 0x3C, 0x3F, 0x41, 0x44, 0x46, 0x49, 0x4B, 0x4D, 0x4F, 0x52, 0x54, 0x56, 0x58, 0x59, 0x5B, 0x5D, 0x5F, 0x60, 0x62, 0x64, 0x65, 0x67, 0x68, 0x69},
{0x00, 0x03, 0x07, 0x0A, 0x0E, 0x11, 0x15, 0x18, 0x1C, 0x1F, 0x22, 0x25, 0x29, 0x2C, 0x2F, 0x32, 0x35, 0x37, 0x3A, 0x3D, 0x40, 0x42, 0x45, 0x47, 0x49, 0x4B, 0x4E, 0x50, 0x52, 0x54, 0x56, 0x58, 0x59, 0x5B, 0x5D, 0x5F, 0x60, 0x62, 0x63, 0x65, 0x66, 0x68},
{0x00, 0x03, 0x06, 0x0A, 0x0D, 0x11, 0x14, 0x17, 0x1B, 0x1E, 0x21, 0x24, 0x27, 0x2B, 0x2D, 0x30, 0x33, 0x36, 0x39, 0x3B, 0x3E, 0x40, 0x43, 0x45, 0x48, 0x4A, 0x4C, 0x4E, 0x50, 0x52, 0x54, 0x56, 0x58, 0x59, 0x5B, 0x5D, 0x5E, 0x60, 0x62, 0x63, 0x64, 0x66},
{0x00, 0x03, 0x06, 0x0A, 0x0D, 0x10, 0x14, 0x17, 0x1A, 0x1D, 0x20, 0x23, 0x26, 0x29, 0x2C, 0x2F, 0x32, 0x35, 0x37, 0x3A, 0x3C, 0x3F, 0x41, 0x44, 0x46, 0x48, 0x4A, 0x4C, 0x4E, 0x50, 0x52, 0x54, 0x56, 0x58, 0x59, 0x5B, 0x5D, 0x5E, 0x60, 0x61, 0x63, 0x64},
{0x00, 0x03, 0x06, 0x09, 0x0D, 0x10, 0x13, 0x16, 0x19, 0x1C, 0x1F, 0x22, 0x25, 0x28, 0x2B, 0x2E, 0x31, 0x33, 0x36, 0x38, 0x3B, 0x3D, 0x40, 0x42, 0x44, 0x47, 0x49, 0x4B, 0x4D, 0x4F, 0x51, 0x53, 0x54, 0x56, 0x58, 0x59, 0x5B, 0x5D, 0x5E, 0x60, 0x61, 0x63},
{0x00, 0x03, 0x06, 0x09, 0x0C, 0x0F, 0x12, 0x16, 0x19, 0x1C, 0x1F, 0x21, 0x24, 0x27, 0x2A, 0x2D, 0x2F, 0x32, 0x35, 0x37, 0x3A, 0x3C, 0x3E, 0x41, 0x43, 0x45, 0x47, 0x49, 0x4B, 0x4D, 0x4F, 0x51, 0x53, 0x55, 0x56, 0x58, 0x59, 0x5B, 0x5D, 0x5E, 0x60, 0x61},
{0x00, 0x03, 0x06, 0x09, 0x0C, 0x0F, 0x12, 0x15, 0x18, 0x1B, 0x1E, 0x21, 0x23, 0x26, 0x29, 0x2C, 0x2E, 0x31, 0x33, 0x36, 0x38, 0x3B, 0x3D, 0x3F, 0x41, 0x44, 0x46, 0x48, 0x4A, 0x4C, 0x4E, 0x4F, 0x51, 0x53, 0x55, 0x56, 0x58, 0x59, 0x5B, 0x5D, 0x5E, 0x5F},
{0x00, 0x03, 0x06, 0x09, 0x0C, 0x0E, 0x11, 0x14, 0x17, 0x1A, 0x1D, 0x20, 0x23, 0x25, 0x28, 0x2B, 0x2D, 0x30, 0x32, 0x35, 0x37, 0x39, 0x3C, 0x3E, 0x40, 0x42, 0x44, 0x46, 0x48, 0x4A, 0x4C, 0x4E, 0x50, 0x51, 0x53, 0x55, 0x56, 0x58, 0x59, 0x5B, 0x5C, 0x5E},
{0x00, 0x02, 0x05, 0x08, 0x0B, 0x0E, 0x11, 0x14, 0x17, 0x19, 0x1C, 0x1F, 0x22, 0x24, 0x27, 0x2A, 0x2C, 0x2F, 0x31, 0x33, 0x36, 0x38, 0x3A, 0x3D, 0x3F, 0x41, 0x43, 0x45, 0x47, 0x49, 0x4B, 0x4C, 0x4E, 0x50, 0x52, 0x53, 0x55, 0x56, 0x58, 0x59, 0x5B, 0x5C},
{0x00, 0x02, 0x05, 0x08, 0x0B, 0x0E, 0x11, 0x13, 0x16, 0x19, 0x1C, 0x1E, 0x21, 0x24, 0x26, 0x29, 0x2B, 0x2E, 0x30, 0x32, 0x35, 0x37, 0x39, 0x3B, 0x3D, 0x40, 0x42, 0x44, 0x45, 0x47, 0x49, 0x4B, 0x4D, 0x4F, 0x50, 0x52, 0x53, 0x55, 0x57, 0x58, 0x59, 0x5B},
{0x00, 0x02, 0x05, 0x08, 0x0B, 0x0D, 0x10, 0x13, 0x16, 0x18, 0x1B, 0x1E, 0x20, 0x23, 0x25, 0x28, 0x2A, 0x2D, 0x2F, 0x31, 0x34, 0x36, 0x38, 0x3A, 0x3C, 0x3E, 0x40, 0x42, 0x44, 0x46, 0x48, 0x4A, 0x4B, 0x4D, 0x4F, 0x50, 0x52, 0x54, 0x55, 0x57, 0x58, 0x59},
};

static inline uint16_t _kbRGB_Angle_Only(float x, float y, float cx, float cy)
{
    double angle    = (atan2(x - cx, y - cy)) * 180 / 3.14159265359;
    const float kbRGB_angls_pre = (float)(0x1<<KBRGB_XY2ANGLE_PRE);
    uint16_t Angle = (uint16_t)(angle*kbRGB_angls_pre);     // 90*8 = 720:10bit[1024]
    return Angle;
}
/*************************************************************************************************
* 为节省空间占用,仅计算 0-90°范围,其它角度根据象限间的关系重新计算
* 为节保留 精度并加快运算速度,将处理后的结果保存为整型，精度为 1/2
**************************************************************************************************/
static void kbRGB_Angle_Only(const uint16_t cols, const uint16_t rows)
{
    float cx = 0;//(cols - 1) * 0.5;
    float cy = 0;//(rows - 1) * 0.5;
    uint8_t matrix[128][128];
    uint16_t col_id, row_id;
    for (row_id = 0; row_id < rows; row_id++)
    {
        for (col_id = 0; col_id < cols; col_id++)
        {
            matrix[row_id][col_id] = _kbRGB_Angle_Only(col_id, row_id, cx, cy);
        }
    }
    printf("static const uint8_t XY2Angle[KBRGB_ANGLE_SIZE*2][KBRGB_ANGLE_SIZE*2] = {\r\n");
    for (row_id = 0; row_id < rows; row_id++)
    {
        printf("{");
        for (col_id = 0; col_id < cols; col_id++)
        {
            //printf("%4d, ",  (int)matrix[y][x].angle);
            //printf("0x%04X, ",  matrix[y][x].pixel);
            printf("0x%02X",  matrix[row_id][col_id]);
            if(col_id<(cols-1)) printf(", ");
        }
        printf("},\r\n");
    }
    printf("};\r\n");
#if 1
    printf("static const uint16_t angle[MATRIX_ANGLE_SIZE][MATRIX_ANGLE_SIZE] = {\r\n");
    for (row_id = 0; row_id < rows; row_id++)
    {
        printf("{");
        for (col_id = 0; col_id < cols; col_id++)
        {
            //printf("%4d, ",  (int)matrix[y][x].angle);
            printf("%3d, ",  matrix[row_id][col_id]);
        }
        printf("},\r\n");
    }
    printf("};\r\n");
#endif
    fflush(stdout);
}
void kbRGB_XY2Angle_Int(void)
{
    //kbRGB_Angle(KBRGB_ANGLE_SIZE, KBRGB_ANGLE_SIZE);
    kbRGB_Angle_Only(KBRGB_ANGLE_SIZE*2, KBRGB_ANGLE_SIZE*2);
}

int16_t kbRGB_XY2Angle(const int8_t x, const int8_t y)
{
    const int16_t cols = KBRGB_ANGLE_SIZE*2;
    const int16_t rows = KBRGB_ANGLE_SIZE*2;
    int16_t angle = 0;
    if((x>=cols) || (x<=-cols)) return angle;
    if((y>=rows) || (y<=-rows)) return angle;
    if((x>=0) && (y>=0)) // 第一象限
    {
        angle = XY2Angle[x][y];
    }
    if((x<0) && (y>=0)) // 第二象限
    {
        angle = XY2Angle[-x][y];
        angle = ((uint32_t)180<<KBRGB_XY2ANGLE_PRE) - angle;
    }
    if((x<0) && (y<0)) // 第三象限
    {
        angle = XY2Angle[-x][-y];
        angle = ((uint32_t)180<<KBRGB_XY2ANGLE_PRE) + angle;
    }
    if((x>=0) && (y<0)) // 第四象限
    {
        angle = XY2Angle[x][-y];
        angle = ((uint32_t)360<<KBRGB_XY2ANGLE_PRE) - angle;
    }
    return angle;
}
float CarmackSqrt (float x)
{
       float xhalf = 0.5f * x;

       int i = *(int*)&x;           // get bits for floating VALUE
       i = 0x5f3759df - (i>>1);     // gives initial guess y0
       x = *(float*)&i;             // convert bits BACK to float
       x = x*(1.5f - xhalf*x*x);    // Newton step, repeating increases accuracy
       x = x*(1.5f - xhalf*x*x);    // Newton step, repeating increases accuracy
       x = x*(1.5f - xhalf*x*x);    // Newton step, repeating increases accuracy
       return (1 / x);
}
unsigned short kb_sqrt(unsigned long a)
{
   unsigned long rem = 0;
   unsigned long root = 0;
   unsigned long divisor = 0;
   for(int i=0; i<16; i++)

   {
     root <<= 1;
     rem = ((rem << 2) + (a >> 30));
     a <<= 2;
     divisor = (root<<1) + 1;
     if(divisor <= rem){
       rem -= divisor;
       root++;
     }
   }
   return (unsigned short)(root);
}
uint32_t kbRGB_XY2Radius(const int8_t x, const int8_t y)
{
    int32_t xx = x*256;
    int32_t yy = y*256;
    uint32_t radius = kb_sqrt(xx*xx + yy*yy);
    return radius;
}

